@use "sass:meta";

@function randomNum($min, $max) {
  $rand: random();
  @return $min + floor($rand * (($max - $min) + 1));
}

/**
* 生成随机颜色
*/
@function randomColor($alpha: 1) {
  @return rgba(random(255), random(255), random(255), $alpha);
}

/// 根据颜色深浅，返回对应的文字颜色
@function dynamicColor($color) {
  @if (ligjtness($color) < 0.5) {
    @return #eee;
  } @else {
    @return #111;
  }
}

$snow: (
  amountSnow: 50,
  // approximately 80 snow / 1 viewport
  colorSnow: "multiColor",
  // value = #fff,... rgba,... special( colorSnow: 'multiColor' )
  duration: 10000,
  // set duration
  size: 5,
  // approximately size
  backgroundImage:
    "http://www.flyingstart.uk.com/wp-content/uploads/2014/12/Merry-Christmas-pictures-free.jpg"
    // background image,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
);

// get key
$amountSnow: map-get($snow, "amountSnow");
$colorSnow: map-get($snow, "colorSnow");
$duration: map-get($snow, "duration");
$size: map-get($snow, "size");
$backgroundImage: map-get($snow, "backgroundImage");
// function snow
@function snow($x, $y, $n, $color) {
  $value: 0 0 0 transparent;
  @for $i from 1 through round($n/2) {
    $value: $value,
      random($x) *
        1vw
        ($y * 1 + random(100)) *
        1vh
        random(8) *
        1px
        random(5) *
        1px
        if(
          $color == "multiColor",
          rgb(random(255), random(255), random(255)),
          unquote($color)
        ),
      random($x) *
        -1vw
        ($y * 1 + random(100)) *
        1vh
        random(8) *
        1px
        random(5) *
        1px
        if(
          $color == "multiColor",
          rgb(random(255), random(255), random(255)),
          unquote($color)
        );
  }
  @return $value;
}

@mixin snow($size) {
  @content;
  width: $size * 1px;
  height: $size * 1px;
  margin: auto;
  // top: 0;
  // right: 0;
  // bottom: 0;
  // left: 0;
  inset: 0;
  border-radius: 50%;
  will-change: box-shadow;
  animation: snow $duration * 1ms linear infinite;
  &:nth-child(2) {
    left: 25vw;
    animation-delay: floor($duration/1.5) * 1ms;
  }
  &:nth-child(3) {
    right: 25vw;
    animation-delay: floor($duration/3) * 1ms;
  }
  @keyframes snow {
    0% {
      box-shadow: snow(100, -150, $amountSnow, $colorSnow);
    }
    100% {
      box-shadow: snow(100, 50, $amountSnow, $colorSnow);
    }
  }
}

/**
* 浅色
*/
@function tint($color, $percentage) {
  @return mix(white, $color, $percentage);
}

/**
* 深色
*/
@function shade($color, $percentage) {
  @return mix(black, $color, $percentage);
}

@function map-has-keys($map, $keys...) {
  @each $key in $keys {
    @if not map-has-key($map, $key) {
      @return false;
    }
  }
  @return true;
}

@function percent($num) {
  @return percentage(calc($num / 100));
}

/**
* 裁剪任意边多边形
*/
@function polygon($points, $startPoint: 0) {
  @if ($points < 3) {
    @warn "polygon() requires at least 3 points";
    $points: 3;
  }
  $angle: calc(360deg / $points); // 360deg / 5 = 72deg
  $coords: "";
  @for $point from $startPoint through ($points + $startPoint - 1) {
    $pointAngle: $point * $angle;
    $x: calc(50% + calc(percentage(sin($pointAngle)) / 2));
    $y: calc(50% + calc(percentage(cos($pointAngle)) / 2));
    $coords: $coords + $x + " " + $y;
    @if $point != ($points + $startPoint - 1) {
      $coords: $coords + ", ";
    }
    $point: $point + 1;
  }
  $polygon: "polygon(" + $coords + ")";
  @return unquote($polygon);
}

@function mod-example($color, $number-of-steps, $current-step) {
  $modified-color: adjust-hue(
    $color,
    calc(45 / $number-of-steps * $current-step)
  );
  $modified-color: transparentize(
    $modified-color,
    calc(1 / $number-of-steps * $current-step)
  );
  @return $modified-color;
}

/// 阴影应用
/// @param {color} $color:背景颜色；
/// @param {number} $number-of-steps:阴影数量；
/// @param {number} $step-size:阴影步数；
/// @param {function} $color-modifier:颜色修改函数；
@function shaded(
  $color: black,
  $number-of-steps: 5,
  $step-size: 5,
  $color-modifier: none
) {
  $original-color: $color;
  $box-shadow: #{$step-size + "px"} #{$step-size + "px"} 0 0 $color;
  @if ($number-of-steps > 1) {
    @for $i from 2 through $number-of-steps {
      @if (
        $color-modifier != "none" and meta.function-exists($color-modifier)
      ) {
        $color: meta.call(
          $color-modifier,
          $original-color,
          $number-of-steps,
          $i
        );
      }
      $box-shadow: $box-shadow,
        #{$step-size * $i + "px"} #{$step-size * $i + "px"} 0 $color;
    }
  }
  @return $box-shadow;
}
